
from get_data.origin_data import *
import matplotlib
import matplotlib.pyplot as plt

def picture_drawing(name):
    '''
    :param name: 因子名称
    :return:
    '''
    ls = ['MACD', 'SMA', '八均线', '多头排列']
    df_shock = pd.read_excel(f'因子回测结果\\{name}回测.xlsx', sheet_name='Roll')[['交易日期', '累积净值']]
    df_index = pd.read_excel(f'因子回测结果\\{name}回测.xlsx', sheet_name='Roll')[['交易日期', '指数净值']]
    for i in ls:
        df = pd.read_excel(f'因子回测结果\\{name}回测.xlsx', sheet_name=f'{i}')
        df_indictor = df[['交易日期', '累积净值']].rename(columns={'累积净值':f'{i}'})
        df_index = pd.merge(df_index, df_indictor, how = 'outer', on = '交易日期')
    df = df_index.dropna().reset_index(drop = True)
    df = pd.merge(df, df_shock, how='outer', on='交易日期').rename(columns={'累积净值':'Roll'}).dropna().reset_index(drop = True)
    if name != 'RSJ_5D' or 'RSJ_10D':
        df = df[df['交易日期'] >= '2018-02-01']

    df['指数净值'] = df['指数净值'] / df.iloc[0, 1]
    df['MACD'] = df['MACD'] / df.iloc[0, 2]
    df['SMA'] = df['SMA'] / df.iloc[0, 3]
    df['八均线'] = df['八均线'] / df.iloc[0, 4]
    df['多头排列'] = df['多头排列'] / df.iloc[0, 5]
    df['Roll'] = df['Roll'] / df.iloc[0, 6]

    matplotlib.rcParams['font.sans-serif'] = ['SimHei']
    matplotlib.rcParams['axes.unicode_minus'] = False

    plt.figure(figsize=(10, 8))
    plt.plot(df['交易日期'], df['指数净值'], color='black', label='基准指数')
    selected_ticks = np.arange(0, len(df), 66)  # 选择每10个点显示一个标签，你可以根据实际情况调整
    plt.xticks(selected_ticks, rotation=45, ha='right')  # rotation旋转角度，ha控制水平对齐方式
    plt.plot(df['交易日期'], df.iloc[:, 6], color='purple', label='Roll(90%/20%/21D)')
    plt.plot(df['交易日期'], df.iloc[:, 2], color='red', label='MACD(5D/10D/5D)')
    plt.plot(df['交易日期'], df.iloc[:, 3], color='green', label='SMA(5D/10D)')
    plt.plot(df['交易日期'], df.iloc[:, 4], color='blue', label='八均线')
    plt.plot(df['交易日期'], df.iloc[:, 5], color='yellow', label='多头排列')
    plt.title(f'{name}')
    plt.xlabel('时间')
    plt.ylabel('净值')
    plt.legend(loc='upper left')
    plt.savefig(f'因子净值对比图\\{name}.jpg')
    plt.close()

def net_value_massive_drawing():
    ls = ['RSJ_5D', 'RSJ_10D', 'cov_+ret_pct_5D', 'covprice_mid', 'covtend', 'covskew',
          'IVdelta_mid_5D', 'IVdelta_mid_22D', 'YTMdelta_mid', 'covpremium_mid',
          'covpremium_modified_mid', 'stock_bondpremium_mid', 'IVnan_pct']
    for type in ls:
        picture_drawing(type)